﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace cubeAdjustment
{
    using static Math;

    public partial class PluginB2X : Form
    {
        double e2 = 0.00669438002290;
        double et2 = 0.00673949677548;
        double a = 6378137;
        double c = 6399593.6259;
        double alaph = 1 / 298.3;

        public PluginB2X()
        {
            InitializeComponent();
        }
        private void switch1_Click(object sender, EventArgs e)
        {
            try
            {
                if (combo.Text == "克拉索夫斯基椭球体(北京54坐标系)")
                {
                    MessageBox.Show("该转换暂未收入程序中","错误提示",MessageBoxButtons.OK);
                }
                label.Text = "";
                double B = Convert.ToDouble(textBoxB.Text) * PI / 180;
                bool F = false;
                if (B < -PI / 2 || B > PI / 2)
                {
                    label.Text += "纬度输入错误 ";
                    label.ForeColor = Color.Red;
                    F = true;
                }
                double L = Convert.ToDouble(textBoxL.Text) * PI / 180;
                if (L < -PI || L > PI)
                {
                    label.Text += "经度输入错误 ";
                    label.ForeColor = Color.Red;
                    F = true;
                }
                double H = Convert.ToDouble(textBoxH.Text);
                if (H < -6378137)
                {
                    label.Text += "高程输入错误 ";
                    label.ForeColor = Color.Red;
                    F = true;
                }

                if (F)
                {
                    return;
                }

                double w = Sqrt(1 - e2 * Pow(Sin(B), 2));
                double v = Sqrt(1 + et2 * Pow(Cos(B), 2));

                double b = a * (1 - alaph);
                double N = a / w;

                double x = (N + H) * Cos(B) * Cos(L);
                double y = (N + H) * Cos(B) * Sin(L);
                double z = (N * (1 - e2) + H) * Sin(B);

                textBoxX.Text = x.ToString(".000000");
                textBoxY.Text = y.ToString(".000000");
                textBoxZ.Text = z.ToString(".000000");
            }
            catch (FormatException)
            {
                MessageBox.Show("请重新输入数字", "错误提示");
            }

        }

        private void switch2_Click(object sender, EventArgs e)
        {
            try
            {
                double X = Convert.ToDouble(textBoxX.Text);
                double Y = Convert.ToDouble(textBoxY.Text);
                double Z = Convert.ToDouble(textBoxZ.Text);

                double L = Math.Atan2(Y, X) * 180 / Math.PI;

                double t0 = Z / Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2));
                double tf = t0;
                double k = 1 + et2;
                double P = c * e2 / Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2));

                while (true)
                {
                    double tn = t0 + P * tf / Math.Sqrt(k + Math.Pow(tf, 2));
                    if (Math.Abs(tf - tn) < 1e-20)
                    {
                        break;
                    }
                    tf = tn;
                }

                double B = Math.Atan(tf);

                double V = Math.Sqrt(1 + et2 * Math.Pow(Math.Cos(B), 2));
                double N = c / V;

                double H = Z / Math.Sin(B) - N * (1 - e2);

                this.textBoxB.Text = (B * 180 / Math.PI).ToString(".000000");
                this.textBoxL.Text = L.ToString(".000000");
                this.textBoxH.Text = H.ToString(".000");
            }
            catch (FormatException)
            {
                MessageBox.Show("请重新输入数字", "错误提示");
            }
        }

        private void combo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (combo.Text == "克拉索夫斯基椭球体(北京54坐标系)")
            {
                a = 6378245;
                alaph = 1 / 298.3;
                c = 6399698.9017827110;
                e2 = 0.006693421622966;
                et2 = 0.006738525414683;
            }
            else if (combo.Text == "1975年国际椭球体(1980国家大地坐标系)")
            {
                a = 6378140;
                alaph = 1 / 298.257;
                c = 6399596.6519880105;
                e2 = 0.006694384999588;
                et2 = 0.006739501819473;
            }
            else if (combo.Text == "WGS-84")
            {
                a = 6378137;
                alaph = 1 / 298.257223563;
                c = 6399593.6258;
                e2 = 0.00669437999013;
                et2 = 0.00673949674227;
            }
            else
            {
                a = 6378137;
                alaph = 1 / 298.257222101;
                c = 6399593.6259;
                e2 = 0.00669438002290;
                et2 = 0.00673949677548;
            }
        }
    }
}
